//
//  XFYLocalData.m
//  MyRobotApp
//
//  Created by 小肥羊 on 15/5/29.
//  Copyright (c) 2015年 小肥羊. All rights reserved.
//

#import "XFYLocalData.h"

@implementation XFYLocalData

#pragma mark - 初始化
- (id)initWithPath:(NSString *)string
{
    self = [super init];
    if (self) {
        if (string) {
            _dataPath = DATABASE_PATH(string);
        } else {
            _dataPath = DATABASE_PATH(@"AllRobotData");
        }
        [self initValue];
    }
    return self;
}

- (void)initValue
{
    self.db = [FMDatabase databaseWithPath:self.dataPath];
    [self.db open];
    //创建碎片表 (记录有用的东西)
    NSString *createFragmentTable = @"CREATE TABLE IF NOT EXISTS 'FragmentDataTable' ('FragmentData' VARCHAR PRIMARY KEY NOT NULL UNIQUE)";
    if ([self.db executeUpdate:createFragmentTable]) {
        NSLog(@"创建碎片数据表");
    } else {
        NSLog(@"创建碎片数据表失败");
    }
    //创建时间记忆表 (接受的外来信息)
    NSString *createMessageMemoryTable = @"CREATE TABLE IF NOT EXISTS 'MessageMemoryTable' ('MessageMemoryDate' DATETIME PRIMARY KEY NOT NULL UNIQUE, 'MessageMemoryContent' VARCHAR)";
    if ([self.db executeUpdate:createMessageMemoryTable]) {
        NSLog(@"创建时间记忆表");
    } else {
        NSLog(@"创建时间记忆表失败");
    }
    //创建认知表 (所认知的种类)
    NSString *createCognitionTable = @"CREATE TABLE IF NOT EXISTS 'CognitionTable' ('Cognition'  VARCHAR PRIMARY KEY NOT NULL UNIQUE, 'CognitionTableName' VARCHAR)";
    if ([self.db executeUpdate:createCognitionTable]) {
        NSLog(@"创建认知表");
    } else {
        NSLog(@"创建认知表失败");
    }
    //    //创建订单表
    //    NSString *createOrderTable = @"CREATE  TABLE  IF NOT EXISTS 'HelperOrder' ('OrderID' VARCHAR PRIMARY KEY NOT NULL  UNIQUE , 'ProviderID' VARCHAR , 'TargetID' VARCHAR , 'Content' VARCHAR , 'StartDate' DATETIME , 'EndDate' DATETIME , 'State' INTEGER )";
    //    if ([self.db executeUpdate:createOrderTable]) {
    //        NSLog(@"创建订单表");
    //    } else {
    //        NSLog(@"创建订单表失败");
    //    }
    //    //创建消息表
    //    NSString *createNewsTable = @"CREATE  TABLE  IF NOT EXISTS 'HelperNews' ('NewsID' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL  UNIQUE , 'TargetID' VARCHAR , 'Content' VARCHAR )";
    //    if ([self.db executeUpdate:createNewsTable]) {
    //        NSLog(@"创建消息表");
    //    } else {
    //        NSLog(@"创建消息表失败");
    //    }
    [self.db close];
}

#pragma mark - 开启关闭
- (BOOL)openDB
{
    if ([self.db open]) {
        NSLog(@"数据库打开");
        return YES;
    } else {
        NSLog(@"数据库打开失败");
        return NO;
    }
}
- (BOOL)closeDB
{
    if ([self.db close]) {
        NSLog(@"数据库关闭");
        return YES;
    } else {
        NSLog(@"数据库关闭失败");
        return NO;
    }
}

#pragma mark - 数据操作
/*! @brief 添加碎片数据 */
- (BOOL)addFragmentData:(NSString *)fragmentData {
    if (!fragmentData || [fragmentData isEqualToString:@""]) {
        NSLog(@"不能添加空数据");
        return NO;
    }
    NSString *addFragmentData = @"INSERT INTO 'FragmentDataTable' ('FragmentData') VALUES (?)";
    if ([self.db executeUpdate:addFragmentData,fragmentData]) {
        NSLog(@"添加");
        return YES;
    } else {
        NSLog(@"添加失败");
        return NO;
    }
}
/*! @brief 添加消息记忆数据 */
- (BOOL)addMessageMemory:(NSDate *)memoryDate Content:(NSString *)content
{
    if (!memoryDate || [content isEqualToString:@""]) {
        NSLog(@"不能添加空消息数据");
        return NO;
    }
    NSString *addMessageMemoryData = @"INSERT INTO 'MessageMemoryTable' ('MessageMemoryDate','MessageMemoryContent') VALUES (?,?)";
    if ([self.db executeUpdate:addMessageMemoryData,memoryDate,content]) {
        NSLog(@"添加");
        return YES;
    } else {
        NSLog(@"添加失败");
        return NO;
    }
}
/*! @brief 添加认知类型数据 */
- (BOOL)addCognition:(NSString *)cognition CognitionTableName:(NSString *)tableName
{
    if (!cognition || [cognition isEqualToString:@""]) {
        NSLog(@"不能添加空消息数据");
        return NO;
    }
    if (!tableName || [tableName isEqualToString:@""]) {
        NSLog(@"不能添加空消息数据");
        return NO;
    }
    NSString *addCognition = @"INSERT INTO 'CognitionTable' ('Cognition','CognitionTableName') VALUES (?,?)";
    if ([self.db executeUpdate:addCognition,cognition,tableName])
    {
        NSLog(@"添加认知，并创建认知相应的类目表");
        NSString *createCategoryTable = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('CategoryName'  VARCHAR PRIMARY KEY NOT NULL UNIQUE, 'Judge' VARCHAR)",tableName];
        if (![self.db executeUpdate:createCategoryTable]) {
             NSLog(@"创建类目表失败");
            return NO;
        }
        return YES;
    } else {
        NSLog(@"添加失败");
        return NO;
    }
}

/*! @brief 修改碎片数据 */
- (BOOL)modifyFragmentData:(NSString *)oldData fragmentData:(NSString *)newData {
    if (!newData || [newData isEqualToString:@""]) {
        NSLog(@"不能添加空数据");
        return NO;
    }
    NSString *modifyFragmentData = @"update FragmentDataTable set FragmentData = ? where FragmentData = ?";
    if ([self.db executeUpdate:modifyFragmentData,newData,oldData]) {
        NSLog(@"修改");
        return YES;
    } else {
        NSLog(@"修改失败");
        return NO;
    }
}

/*! @brief 删除碎片数据 */
- (BOOL)deleteFragmentData:(NSString *)data
{
    NSString *queryString = @"delete from FragmentDataTable where FragmentData = ?";
    if ([self.db executeUpdate:queryString,data]) {
        NSLog(@"删除");
        return YES;
    } else {
        NSLog(@"删除失败");
        return NO;
    }
}

/*! @brief 查找碎片数据 */
- (FMResultSet *)searchAllFragmentData {
    NSString *queryString = @"select * from FragmentDataTable";
    return [self.db executeQuery:queryString];
}
/*! @brief 查找消息记忆数据 */
- (FMResultSet *)searchAllMessageMemory
{
    NSString *queryString = @"select * from MessageMemoryTable";
    return [self.db executeQuery:queryString];
}
/*! @brief 查找认知数据 */
- (FMResultSet *)searchAllCognition
{
    NSString *queryString = @"select * from CognitionTable";
    return [self.db executeQuery:queryString];
}

@end
